package com.enation.app.javashop.base.api;

import com.enation.app.javashop.framework.util.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by kingapex on 2018/12/11.
 *
 * @author kingapex
 * @version 1.0
 * @since 7.0.0
 * 2018/12/11
 */
@Service
public class MyTest {

    public static void main(String[] args) {

        String str  = DateUtil.toString(1544580966L,"yyyy-MM-dd HH:MM:ss");
        System.out.println(str);
    }



    AtomicReference<List<Integer>> autoList =null;

    public    String test2() {


        double number = Math.random();
        int num  = (int)(number * 10000);


        if (num % 2 == 0) {
            autoList = new AtomicReference<>(this.getList1());
        } else {
            autoList = new AtomicReference<>(this.getList2());
        }


        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        int index  = autoList.get().get(0)+1;

        if(num%2==0){
            if( index!= 2){
                System.out.println("不对");
            }else {
                System.out.println("对");

            }

        }else {
            if( index!= 3){
                System.out.println("不对");
            }else {
                System.out.println("对");

            }
        }


        return "ok";
    }



    public String test() {

        List<Integer > list =  this.getList1();
        AtomicReference<Integer> index= new AtomicReference<>(0);
        list.forEach( i->{

            if(i==1){
                index.getAndSet(index.get() + 1);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }

        });

        System.out.println(index.get());
        return "ok";
    }

    private List<Integer > getList1(){

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(1);
        }

        return list;

    }

    private List<Integer > getList2(){

        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(2);
        }

        return list;

    }

}
